<template>
    <div>
        <el-form :model="addPermissionForm" :rules="addPermissionFormRules" ref="addPermissionForm" label-width="140px"
            class="demo-ruleForm">
            <el-form-item label="授权码类型">
                <el-select v-model="addPermissionForm.permissionType" placeholder="请选择">
                    <el-option v-for="item in permissionTypeOptions" :key="item.value" :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>


            <el-form-item label="授权码有效值" style="width:20%" v-if="addPermissionForm.permissionType!=3"
                prop="permissionEffective">
                <el-input type="number" placeholder="请输入天数" v-model="addPermissionForm.permissionEffective">
                    <template v-if="addPermissionForm.permissionType==1" slot="append">天</template>
                    <template v-else-if="addPermissionForm.permissionType==2" slot="append">次</template>
                </el-input>
            </el-form-item>

            <el-form-item label="创建个数" style="width:20%" prop="createNum">
                <el-input type="number" placeholder="创建个数" v-model="addPermissionForm.createNum">
                </el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('addPermissionForm')">立即创建</el-button>
                <el-button @click="resetForm('addPermissionForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {
        addPermissionData
    } from "@/api/permissionApi";


    export default {
        data() {
            return {
                addPermissionForm: {
                    permissionType: 1,
                    permissionEffective: null,
                    createNum: null
                },
                addPermissionFormRules: {
                    permissionEffective: [{
                        required: true,
                        message: '请输入有效值',
                        trigger: 'blur'
                    }],
                    createNum: [{
                        required: true,
                        message: '请输入创建个数',
                        trigger: 'blur'
                    }]
                },
                permissionTypeOptions: [{
                    value: 1,
                    label: '时间授权码'
                }, {
                    value: 2,
                    label: '次数授权码'
                }, {
                    value: 3,
                    label: '永久授权码'
                }],
            }
        },
        created: function () {},
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if(this.addPermissionForm.permissionType==3){
                            this.addPermissionForm.permissionEffective=0
                        }
                        addPermissionData(this.addPermissionForm).then(response => {
                           this.$message({
                                showClose: true,
                                message: "授权码创建成功",
                                type: 'success'
                            })
                            this.resetForm(formName)
                        }).catch(error => {
                            this.$message({
                                showClose: true,
                                message: error,
                                type: 'error'
                            })
                        })
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style>
    /*去掉type为number的箭头*/
    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button {
        -webkit-appearance: none;
    }

    input[type="number"] {
        -moz-appearance: textfield;
    }
</style>